package com.youlu.campus.admin.commission;

import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.commission.dto.DistributorChannelCommissionOrderQueryDTO;
import com.youlu.campus.entity.commission.dto.DistributorChannelGmvQueryDTO;
import com.youlu.campus.service.commission.DistributorChannelGmvStatisticsService;
import com.youlu.campus.service.commission.DistributorChannelSharingCommissionService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


/**
 * 渠道分拥订单
 */
@Slf4j
@RestController
public class DistributorChannelSharingCommissionDetailController {

    @Autowired
    private DistributorChannelSharingCommissionService distributorChannelSharingCommissionService;
    @Autowired
    private DistributorChannelGmvStatisticsService distributorChannelGmvStatisticsService;

    @Autowired
    private RoleService roleService;

    private final static String [] EXPORT_ORDER_USER = {"zhuhuaiqi","lihui","liangjinqiu","yangyang","liuyun","liukun","zhoumin"};

    @PostMapping("/distributor/channel/export/commission-order")
    public QResult exportOrder(@RequestBody DistributorChannelCommissionOrderQueryDTO dto){
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> "渠道分销".equals(role.getName())).count();
        if (count > 0) {
          throw new BusinessException("渠道分销不能导出订单");
        }
        String loginName = AuthUtils.getLoginName();
        log.info("【导出订单】loginName:{}",loginName);
        long count2 = Arrays.stream(EXPORT_ORDER_USER).filter(exportUser -> exportUser.equals(loginName)).count();
        if(count2==0){
            throw new BusinessException("对不起，您当前没有权限导出订单数据");
        }
        dto.setOptUserName(AuthUtils.getLoginName());
        dto.setOptUserId(AuthUtils.getUserId());
        distributorChannelSharingCommissionService.exportOrder(dto);
        return QResult.success(null);
    }


    @PostMapping("/distributor/channel/commission-list")
    public QResult list(@RequestBody DistributorChannelCommissionOrderQueryDTO dto) {
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> "渠道分销".equals(role.getName())).count();
        if (count > 0) {
            dto.setDistributorChannelRole(Boolean.TRUE);
            dto.setDistributorChannelUserId(userId);
        }
        log.info("渠道分拥订单 userId:{},是否是渠道分销角色:{}",userId,count);
        return QResult.success(distributorChannelSharingCommissionService.list(dto));
    }

    @PostMapping("/distributor/channel/order-gmv/statistics")
    public QResult listGmvStatistics(@RequestBody DistributorChannelGmvQueryDTO  dto) {
        String userId = AuthUtils.getUserId();
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> "渠道分销".equals(role.getName())).count();
        if (count > 0) {
            return QResult.success(null);
        }
        log.info("渠道分拥订单 userId:{},是否是渠道分销角色:{}",userId,count);
        return QResult.success(distributorChannelGmvStatisticsService.list(dto));
    }
}
